﻿using System;
using System.Data;
using System.Data.SqlServerCe;
using System.IO;
using System.Globalization;

namespace MyMegafon
{
    class Database
    {
        private const string DatabaseName = "Data.sdf";
        private static string ConnectString = "Data Source = " +
            Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
             + "\\" + DatabaseName + "; Password = 007";
        private const string DataTableName = "T";
        private const string DateTimeColumn = "DTIME";
        public const string BalanceColumn = "BALANCE";

        static public void CreateDatabaseIfNonExistant()
        {
            if (File.Exists(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
             + "\\" + DatabaseName) == false)
            {
                CreateDatabase();
            }
        }

        private static void CreateDatabase()
        {
            // Создаем базу
            SqlCeEngine sqlCeEngine = null;
            SqlCeConnection connection = null;
            try
            {
                sqlCeEngine = new SqlCeEngine(ConnectString);
                sqlCeEngine.CreateDatabase();

            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.ToString());
            }
            finally
            {
                if (sqlCeEngine != null)
                    sqlCeEngine.Dispose();

            }
            try
            {
                connection = new SqlCeConnection(ConnectString);
                connection.Open();
                SqlCeCommand cmd = connection.CreateCommand();
                cmd.CommandText = "CREATE TABLE " + DataTableName + " (" +
                                  DateTimeColumn + " DateTime" + ", " +
                                  BalanceColumn + " nvarchar(10)" + ")";
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.ToString());
            }
            finally
            {
                if (connection != null) connection.Close();
                System.Windows.Forms.MessageBox.Show("База создана.");
            }
        }

        public static void SaveToDatabase(double balance)
        {
            CreateDatabaseIfNonExistant();
            SqlCeConnection connection = null;
            SqlCeCommand cmd = null;
            try
            {
                connection = new SqlCeConnection(ConnectString);
                connection.Open();
                cmd = connection.CreateCommand();
                cmd.Parameters.Add("@D", SqlDbType.DateTime);
                cmd.Parameters["@D"].Value = DateTime.Now;
                cmd.CommandText = "INSERT INTO " + DataTableName + " (" +
                                  DateTimeColumn + ", " +
                                  BalanceColumn + " ) VALUES ("
                                  + "@D, '"
                                  + balance + "')";
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {

                System.Windows.Forms.MessageBox.Show("Ошибка сохранения данных в бд " + e);
            }
            finally
            {
                if (cmd != null) cmd.Dispose();
                if (connection != null) connection.Close();
            }
        }
    }
}
